package com.million.artservice.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.million.artservice.entity.params.CommentParams;
import com.million.artservice.entity.vo.CommentVo;
import com.million.artservice.entity.vo.QueryCommentPageVo;
import com.million.artservice.entity.vo.QueryCommentVo;
import com.million.artservice.service.ArticleCommentService;
import com.million.commonutils.R;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 文章评论 前端控制器
 * </p>
 *
 * @author 十麓
 * @since 2022-04-17
 */
@RestController
@RequestMapping("/artservice/comment")
public class ArticleCommentController {

    @Autowired
    private ArticleCommentService articleCommentService;

    //根据文章ID显示文章评论列表
    @GetMapping("queryCommentVoList/{articleId}")
    public R queryCommentVoList (@PathVariable("articleId") String articleId) {
        List<CommentVo> commentVoList = articleCommentService.queryCommentVoList(articleId);
        return R.ok().data("commentVoList", commentVoList);
    }

    //添加评论
    @PostMapping("addComment")
    public R addComment (@RequestBody CommentParams commentParams, HttpServletRequest request) {
        String commentId = articleCommentService.addComment(commentParams, request);
        if (StringUtils.isEmpty(commentId)) {
            return R.error().code(28004).message("请先登录");
        }
        return R.ok();
    }

    //分页条件查询文章评论列表  根据文章id  或者 开始时间到结束时间
    @PostMapping("queryCommentVoPage/{page}/{limit}")
    public R queryCommentVoPage (@ApiParam(name = "page", value = "当前页码", required = true)
                                 @PathVariable("page") Long page,
                                 @ApiParam(name = "limit", value = "每页记录数", required = true)
                                 @PathVariable("limit") Long limit,
                                 @RequestBody QueryCommentVo queryCommentVo) {
        Page<QueryCommentPageVo> commentPageVoPage = new Page<>(page, limit);
        Page<QueryCommentPageVo> pageVoPage = articleCommentService.queryCommentVoPage(commentPageVoPage, queryCommentVo);
        return R.ok().data("pageVoPage",pageVoPage);
    }

    //根据文章评论id 删除评论信息
    @DeleteMapping("deleteCommentInfo/{commentId}")
    public R deleteCommentInfo(@PathVariable("commentId") String commentId){
        articleCommentService.deleteCommentInfo(commentId);
        return R.ok();
    }
}

